<?php

namespace app\admin\controller\system;

use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\SystemCity;
use think\App;
use EasyAdmin\annotation\ControllerAnnotation;

/**
 * @ControllerAnnotation(title="省")
 */
class Province extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\SystemProvince();

    }

    /**
     * @Annotation(title="获取所有省市区")
     * @return array|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProvinceCityByListTree()
    {
        //获取所有城市
        try {
            $row = $this->model->getProvinceCityAreaByListTree("id,province_id as value,province as name");
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, "查询失败");
        }
        $lists = [];
        if (!empty($row)) {
            foreach ($row as $k => $v) {
                $lists[$k] = $v;
                $city = (new SystemCity())->getCityByProvinceList($v['value']);
                $lists[$k]['children'] = $city ?? [];
            }
        }
        return Show::json_show(Status::status_normal, "ok", $lists);
    }

    public function getProvinceByList()
    {
        $row = $this->model->getBaseByListAll(['status' => 1], 'province_id as value,province as name', 'sort desc,id asc');
        return Show::json_show(Status::status_normal, "ok", $row);
    }
}